﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RookieLibSharp.Configuration;
using RookieLibSharp.WinApi;
namespace BirthdayRemainder
{
    public partial class FrmAddInfo : Form
    {

        public IntPtr ParantHwnd { get; set; }
        public FrmAddInfo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 重绘窗体，画一条直线
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Point p1 = new Point(0, this.btnAdd.Location.Y - 10);
            Size size = new Size(this.Width, 2);
            Rectangle rect = new Rectangle(p1, size);
            e.Graphics.FillRectangle((Brush)Brushes.DarkGray, rect);
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!ValidateCheck())
            {
                return;
            }
            AddInfo();

        }
        private bool ValidateCheck()
        {
            if (!DbMgr.DatabaseFileExsit())
            {
                MessageBox.Show("先联网初始化数据库", "提醒", MessageBoxButtons.OK);
                return false;
            }
            if (tbName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写姓名", "提醒", MessageBoxButtons.OK);
                return false;
            }
            if ((tbPhne.Text != "") && (!Funcs.IsPhoneNum(tbPhne.Text)))
            {
                MessageBox.Show("请正确填写手机号码", "提醒", MessageBoxButtons.OK);
                return false;
            }
            if (SftMgr.DbNameIsExit(tbName.Text))
            {
                MessageBox.Show("该姓名已经存在，不能添加\n如需修改，请在主界面点击【修改】按钮进行修改", "提醒", MessageBoxButtons.OK);
                return false;
            }
            return true;
        }

        private void FrmAddInfo_Load(object sender, EventArgs e)
        {
            this.Icon = BirthdayRemainder.Properties.Resources.ico_main;
            this.cbbSolorLunar.SelectedIndex = 0;
            this.dtpBirthday.MaxDate = DateTime.Today.AddDays(-1);
        }
        private void AddInfo()
        {
            PersonInfo info = new PersonInfo();
            info.Name = tbName.Text;
            info.Birthday = dtpBirthday.Value.ToString(GlobalValue.STR_DATE_TIME_FORMAT);
            info.Phone = tbPhne.Text;
            info.IsLunar = cbbSolorLunar.Text == "阴历";
            bool bRet = SftMgr.DbInsertRecord(info);
            StPersonInfo stInfo = info.GetStruct();
            Win32Api.SendMessage(ParantHwnd, (int)WM_MSG.WM_MSG_LIST_ADD_ITEM, IntPtr.Zero, (object)stInfo);
            if (bRet)
            {
                MessageBox.Show("添加成功", "提醒", MessageBoxButtons.OK);
                bool bCloseFrm = Cfg.ReadBool(GlobalValue.SFT_CFG, GlobalValue.SFT_CFG_CLOSE_FORM_AFTER_ADD);
                if (bCloseFrm)
                {
                    CloseFrm();
                }
                else
                {
                    Win32Api.SendMessage(ParantHwnd, (int)WM_MSG.WM_MSG_LIST_INIT, IntPtr.Zero, IntPtr.Zero);
                }
            }
            else
            {
                MessageBox.Show("添加失败", "提醒", MessageBoxButtons.OK);
            }
        }
        private delegate void dl_CloseFrm();
        private void CloseFrm()
        {
            if (this.InvokeRequired)
            {
                dl_CloseFrm method = CloseFrm;
                Invoke(method);
                return;
            }
            this.Close();
        }
    }
}
